﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CanvasEx.DrawCore
{
    public class DrawArc : DrawObject
    {
        public Point center, sta, end;
        public double r;
        public SweepDirection dir;



        private PathFigure figure = new PathFigure();
        private ArcSegment arc = new ArcSegment();
        private PathGeometry pathGeometry = new PathGeometry();
        
        public DrawArc(Painter p, Point sta, Point end, Point center, SweepDirection dir)
            : base(p)
        {
            this.sta = sta;
            this.end = end;
            this.center = center;
            this.dir = dir;

            bool isLargeArc = false;
            r = DrawHelper.Getdistance(sta, center);
            Point midpoint = new Point((sta.X + end.X) / 2, (sta.Y + end.Y) / 2);
            if (center.X > midpoint.X)
                if (dir == SweepDirection.Counterclockwise)
                    isLargeArc = false;
                else
                    isLargeArc = true;
            else if (center.X < midpoint.X)
                if (dir == SweepDirection.Counterclockwise)
                    isLargeArc = true;
                else
                    isLargeArc = false;
            else
                if (dir == SweepDirection.Counterclockwise)
                    isLargeArc = center.Y < midpoint.Y ? true : false;
                else
                    isLargeArc = center.Y < midpoint.Y ? false : true;

            figure.StartPoint = sta;
            arc = new ArcSegment(end, new Size(r, r), 0, isLargeArc, dir, true);
            figure.Segments.Add(arc);
            pathGeometry.Figures.Add(figure);
            path.Data = pathGeometry;
        }

        public override Geometry GetGeometry()
        {
            return pathGeometry;
        }

        public override bool InGraph(Point Location, double SelectAccuracy)
        {
            if (Math.Abs(DrawHelper.Getdistance(Location, center) - r) > SelectAccuracy)//不在圆弧所在的圆上
            {
                return false;
            }

            Point midpoint = new Point((sta.X + end.X) / 2, (sta.Y + end.Y) / 2);
            double arclen = DrawHelper.Getdistance(midpoint, sta);
            if (arclen == r)//圆弧为半圆时
            {
                Vector v = sta - center;
                
                double k = -1 / ((sta - end).Y / (sta - end).X);
                double b = center.Y - k * center.X;
                Point AssistPoints = new Point();
                //if (v.X > 0 && v.Y > 0)//第一象限
                //{
                //    if (dir == SweepDirection.Counterclockwise)
                //        AssistPoints = new Point(center.X - 10, k * (center.X - 10) + b);
                //    else
                //        AssistPoints = new Point(center.X + 10, k * (center.X + 10) + b);
                //}
                //if (v.X < 0 && v.Y > 0)//第二象限
                //{
                //    if (dir == SweepDirection.Counterclockwise)
                //        AssistPoints = new Point(center.X - 10, k * (center.X - 10) + b);
                //    else
                //        AssistPoints = new Point(center.X + 10, k * (center.X + 10) + b);
                //}
                //if (v.X < 0 && v.Y < 0)//第三象限
                //{
                //    if (dir == SweepDirection.Counterclockwise)
                //        AssistPoints = new Point(center.X + 10, k * (center.X + 10) + b);
                //    else
                //        AssistPoints = new Point(center.X - 10, k * (center.X - 10) + b);
                //}
                //if (v.X > 0 && v.Y < 0)//第四象限
                //{
                //    if (dir == SweepDirection.Counterclockwise)
                //        AssistPoints = new Point(center.X + 10, k * (center.X + 10) + b);
                //    else
                //        AssistPoints = new Point(center.X - 10, k * (center.X - 10) + b);
                //}
                if (v.Y < 0)//第三、四象限
                {
                    if (dir == SweepDirection.Counterclockwise)
                        AssistPoints = new Point(center.X - 10, k * (center.X - 10) + b);
                    else
                        AssistPoints = new Point(center.X + 10, k * (center.X + 10) + b);
                }
                if (v.Y > 0)//第一、二象限
                {
                    if (dir == SweepDirection.Counterclockwise)
                        AssistPoints = new Point(center.X + 10, k * (center.X + 10) + b);
                    else
                        AssistPoints = new Point(center.X - 10, k * (center.X - 10) + b);
                }
                if (v.Y == 0 && v.X < 0)
                {
                    if (dir == SweepDirection.Counterclockwise)
                        AssistPoints = new Point(center.X, center.Y - 10);
                    else
                        AssistPoints = new Point(center.X, center.Y + 10);
                }
                if (v.Y == 0 && v.X > 0)
                {
                    if (dir == SweepDirection.Counterclockwise)
                        AssistPoints = new Point(center.X, center.Y + 10);
                    else
                        AssistPoints = new Point(center.X, center.Y - 10);
                }

                if (DrawHelper.Getdistance(AssistPoints, sta) < DrawHelper.Getdistance(AssistPoints, Location))
                    return false;
                else
                    return true;
            }
            if (arc.IsLargeArc == true)
            {
                if (DrawHelper.Getdistance(Location, midpoint) - arclen >= 0)
                    return true;
                else
                    return false;
            }
            else
            {
                if (DrawHelper.Getdistance(Location, midpoint) - arclen <= 0)
                    return true;
                else
                    return false;
            }
        }
        public override List<Point> GetHotSpot()
        {
            return new List<Point>() { sta, end, center };
        }
    }
}
